/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import me.zhengjie.annotation.Log;
import me.zhengjie.annotation.rest.AnonymousGetMapping;
import me.zhengjie.annotation.rest.AnonymousPostMapping;
import me.zhengjie.modules.system.domain.PokerServer;
import me.zhengjie.modules.system.domain.PokerUserServerInfo;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.service.PokerServerService;
import me.zhengjie.modules.system.domain.dto.PokerServerQueryCriteria;
import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.List;

import me.zhengjie.modules.system.service.PokerUserServerInfoService;
import me.zhengjie.modules.system.service.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author greazy
* @date 2025-04-10
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "server")
@RequestMapping("/api/pokerServer")
public class PokerServerController {

    private final PokerServerService pokerServerService;

    private final UserService userService;

    private final PokerUserServerInfoService userServerInfoService;

    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('pokerServer:list')")
    public void exportPokerServer(HttpServletResponse response, PokerServerQueryCriteria criteria) throws IOException {
        pokerServerService.download(pokerServerService.queryAll(criteria), response);
    }

    @GetMapping
    @ApiOperation("查询server")
    @PreAuthorize("@el.check('pokerServer:list')")
    public ResponseEntity<PageResult<PokerServer>> queryPokerServer(PokerServerQueryCriteria criteria){
        Page<Object> page = new Page<>(criteria.getPage(), criteria.getSize());
        return new ResponseEntity<>(pokerServerService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增server")
    @ApiOperation("新增server")
    @PreAuthorize("@el.check('pokerServer:add')")
    public ResponseEntity<Object> createPokerServer(@Validated @RequestBody PokerServer resources){
        pokerServerService.create(resources);
        List<User> list = userService.list();
        ArrayList<PokerUserServerInfo> insertList = new ArrayList<>();
        for (User user : list) {
            PokerUserServerInfo userServerInfo = new PokerUserServerInfo();
            userServerInfo.setServerId(resources.getId());
            userServerInfo.setUserId(user.getId());
            userServerInfo.setJettonNum(0);
            insertList.add(userServerInfo);
        }
        userServerInfoService.saveBatch(insertList);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改server")
    @ApiOperation("修改server")
    @PreAuthorize("@el.check('pokerServer:edit')")
    public ResponseEntity<Object> updatePokerServer(@Validated @RequestBody PokerServer resources){
        pokerServerService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除server")
    @ApiOperation("删除server")
    @PreAuthorize("@el.check('pokerServer:del')")
    public ResponseEntity<Object> deletePokerServer(@ApiParam(value = "传ID数组[]") @RequestBody List<Integer> ids) {
        pokerServerService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @AnonymousPostMapping("/dataSync")
    public ResponseEntity<Object> dataSync(){
        pokerServerService.dataSync();
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @AnonymousGetMapping("/serverList")
    public ResponseEntity<Object> serverList(){
        LambdaQueryWrapper<PokerServer> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(PokerServer::getId);
        List<PokerServer> list = pokerServerService.list(wrapper);
        for (PokerServer pokerServer : list) {
            pokerServer.setIpAddr("");
            pokerServer.setPortAddr("");
            pokerServer.setPasswordRcon("");
        }
        return new ResponseEntity<>(list,HttpStatus.OK);
    }
}